/**********************************************************************

    SampleDialog.cp

***********************************************************************/

/*
    A sample case demonstrating a movable model dialog box.  Need 
    System 7 for pop-up menu functionality.
*/

/********** Includes */
#include <Dialogs.h>
#include "Fn_Prototypes.h"
#include "MainWindow.h"

/********** Defines */
#define SAMPLE_DLOG    500
#define NIL_PTR        0L
#define ALLOCATE_MEM   0
#define IN_FRONT       (WindowPtr)-1L
#define RETURN_KEY     13
#define ENTER_KEY      3
#define ESCAPE_KEY     27
#define PERIOD_KEY     46
#define VISUAL_DELAY   8  // standard is 8 ticks

#define OK_BUTTON      1
#define CANCEL_BUTTON  2
#define NAME           5
#define PHONE          6
#define MALE_RADIO     7
#define FEMALE_RADIO   8
#define MARRIED_BOX    10
#define DB             11

/********** Prototypes */
Boolean        MySampleDialog( void );
pascal Boolean MyDialogEventFilter( DialogPtr    theDialog,
                                    EventRecord  *theEvent,
                                    short        *itemHit );

/********** SampleDialog */

Boolean MySampleDialog( void )

    WindowPtr   docWindow;
    DialogPtr   dialog;
    Boolean     done;
    Boolean     result;
    short       itemHit;
    short       itemType;
    Handle      itemHandle;
    Rect        itemRect;
    EventRecord theEvent;
    
    Boolean     isMale;
    Boolean     isFemale;
    Boolean     isMarried;
    int         phoneDB;
    Str255      name;
    Str255      phone;

    result = FALSE;

    docWindow = FrontWindow();
    if( docWindow != NIL_PTR )
        MyDoDeactivateWindow( docWindow );

    dialog = GetNewDialog( SAMPLE_DLOG, ALLOCATE_MEM, IN_FRONT );

    if( dialog == NIL_PTR )
        return( result );

    /* AdjustMenus_(); */
    ShowWindow( dialog );
    FnMisc_FrameButton( dialog, OK_BUTTON );

    done = FALSE;
    while( done == FALSE )
    
        ModalDialog( &MyDialogEventFilter, &itemHit );

        switch( itemHit )
        
            case OK_BUTTON:
                result = TRUE;
                done = TRUE;
                break;
            case CANCEL_BUTTON:
                done = TRUE;
                break;
            case MALE_RADIO:
                GetDItem( dialog,
                          MALE_RADIO,
                          &itemType,
                          &itemHandle,
                          &itemRect );
                SetCtlValue( (ControlHandle)itemHandle, TRUE );
                GetDItem( dialog,
                          FEMALE_RADIO,
                          &itemType,
                          &itemHandle,
                          &itemRect );
                SetCtlValue( (ControlHandle)itemHandle, FALSE );
                break;
            case FEMALE_RADIO:
                GetDItem( dialog,
                          FEMALE_RADIO,
                          &itemType,
                          &itemHandle,
                          &itemRect );
                SetCtlValue( (ControlHandle)itemHandle, TRUE );
                GetDItem( dialog,
                          MALE_RADIO,
                          &itemType,
                          &itemHandle,
                          &itemRect );
                SetCtlValue( (ControlHandle)itemHandle, FALSE );
                break;
            case MARRIED_BOX:
                GetDItem( dialog,
                          MARRIED_BOX,
                          &itemType,
                          &itemHandle,
                          &itemRect );
                if( GetCtlValue( (ControlHandle)itemHandle ) )
                    SetCtlValue( (ControlHandle)itemHandle, FALSE );
                else
                    SetCtlValue( (ControlHandle)itemHandle, TRUE );
                break;

    GetDItem(dialog,MALE_RADIO,&itemType,&itemHandle,&itemRect);
    isMale = GetCtlValue((ControlHandle)itemHandle);
    GetDItem(dialog,FEMALE_RADIO,&itemType,&itemHandle,&itemRect);
    isFemale = GetCtlValue((ControlHandle)itemHandle);
    GetDItem(dialog,MARRIED_BOX,&itemType,&itemHandle,&itemRect);
    isMarried = GetCtlValue((ControlHandle)itemHandle);

    GetDItem(dialog,NAME,&itemType,&itemHandle,&itemRect);
    GetIText(itemHandle,name);
    GetDItem(dialog,PHONE,&itemType,&itemHandle,&itemRect);
    GetIText(itemHandle,phone);

    GetDItem(dialog,DB,&itemType,&itemHandle,&itemRect);
    phoneDB = GetCtlValue((ControlHandle)itemHandle);

    DisposDialog( dialog );

    return( result );

/********** PASCAL MyDialogEventFilter */

pascal Boolean MyDialogEventFilter( DialogPtr    theDialog,
                                    EventRecord  *theEvent,
                                    short        *itemHit )

    short          thePart;
    char           key;
    short          itemType;
    Handle         itemHandle;
    Rect           itemRect;
    long           finalTicks;
    Rect           dragRect;
    Boolean        result;
    WindowPtr      theWindow;
    
    result = FALSE;
    dragRect = qd.screenBits.bounds;

        switch( (*theEvent).what )
        
            case mouseDown:
                thePart = FindWindow( (*theEvent).where, &theWindow );
                if( theWindow == theDialog )
                
                switch( thePart )
                    
                        case inDrag:
                            DragWindow( theDialog,
                                        (*theEvent).where,
                                        &dragRect );
                            result = TRUE;
                            break;

                break;
            case keyDown:
            case autoKey:
                key = (*theEvent).message & charCodeMask;
                if( (key == RETURN_KEY) || (key == ENTER_KEY) )
                
                    *itemHit = OK_BUTTON;
                    GetDItem( theDialog,
                              OK_BUTTON,
                              &itemType,
                              &itemHandle,
                              &itemRect );
                    HiliteControl( (ControlHandle)itemHandle,
                                   inButton );
                    Delay( VISUAL_DELAY, &finalTicks );
                    HiliteControl( (ControlHandle)itemHandle, 0 );
                    result = TRUE;
                
                if( (key == ESCAPE_KEY) ||
                    (((*theEvent).modifiers & cmdKey) &&
                    (key == PERIOD_KEY)) )
                
                    *itemHit = CANCEL_BUTTON;
                    GetDItem( theDialog,
                              CANCEL_BUTTON,
                              &itemType,
                              &itemHandle,
                              &itemRect );
                    HiliteControl( (ControlHandle)itemHandle,
                                   inButton );
                    Delay( VISUAL_DELAY, &finalTicks );
                    HiliteControl( (ControlHandle)itemHandle, 0 );
                    result = TRUE;
                
                /* Handle other keyboard equivalents here */
                break;
            case updateEvt:
                if( (WindowPtr)(*theEvent).message != theDialog )
                
                    MyDoUpdateWindow( (WindowPtr)(*theEvent).message );
                
                else
                
                    FnMisc_FrameButton( theDialog, OK_BUTTON );
                
                break;
            case activateEvt:
                if( (WindowPtr)(*theEvent).message != theDialog )
                
                    /*
                    DoActivate_( (WindowPtr)(*theEvent).message,
                                 ((*theEvent).modifiers & activeFlag),
                                 *theEvent );
                    */
                 
                 break;

    return( result );

/**********************************************************************

    SampleDialog.r

***********************************************************************/

#include <Types.r>
#include <BalloonTypes.r>

resource 'CNTL' (500, "Popup", purgeable) 

90, 40, 109, 282,

popupTitleLeftJust,

visible,

80,  /* pixel width of title */

500, /* MENU resource ID */

popupMenuCDEFProc,

0,   /* reference value */

"Database:"
;

resource 'MENU' (500, "Popup Items") 

500,  /* menu ID */

textMenuProc,

allEnabled,

enabled,

"Popup",

"Yellow Pages", noIcon, nokey, noMark, plain,

"White Pages", noIcon, nokey, noMark, plain,

"Personal Database", noIcon, nokey, noMark, plain

;

resource 'DLOG' (500, "Sample Dialog", purgeable) 

46, 10, 195, 460,

movableDBoxProc,

invisible,

noGoAway,

0x0,

500,

"Sample Dialog"
;

resource 'DITL' (500, "Sample Dialog", purgeable) 

/* array DITLarray: 11 elements */

/* [1] */

119, 382, 139, 440,

Button 

enabled,

"OK"

,

/* [2] */

119, 311, 139, 369,

Button 

enabled,

"Cancel"

,

/* [3] */

20, 30, 36, 86,

StaticText 

disabled,

"Name"

,

/* [4] */

50, 30, 66, 86,

StaticText 

disabled,

"Phone"

,

/* [5] */

20, 90, 36, 288,

EditText 

enabled,

""

,

/* [6] */

50, 90, 66, 288,

EditText 

enabled,

""

,

/* [7] */

20, 340, 38, 446,

RadioButton 

enabled,

"Male"

,

/* [8] */

40, 340, 58, 446,

RadioButton 

enabled,

"Female"

,

/* [9] */

60, 350, 76, 425,

StaticText 

disabled,

"_______"

,

/* [10] */

80, 340, 98, 446,

CheckBox 

enabled,

"Married"

,

/* [11] */

90, 40, 109, 282,

Control 

enabled,

500

,

0, 0, 0, 0,

HelpItem 

  disabled,

  HMScanhdlg

  500

;

/**********************************************************************

    SampleDialogBalloons.r

***********************************************************************/

#include <Types.r>
#include <BalloonTypes.r>

resource 'hdlg' (500, "Sample Dialog Balloons", purgeable)
  /* Header */
  HelpMgrVersion,
  0,                  /* start help with first item in DITL */
  hmDefaultOptions,   /* hmDefaultOptions or hmSaveBitsNoWindow */
  0,                  /* balloon definition */
  5,                  /* variation code or position code, reference
                         IM: More Macintosh Toolbox p3-10 */
  
  /* Missing Component */
  HMSkipItem 
  ,
  
  /* Help */
  
    /* [1] */
    HMStringREsItem  /* store help messages in STR# 500 */
     10, 10 ,       /* default tip location is  0, 0  */
     0, 0, 0, 0 ,   /* default alternate rectangle */
    500, 1,           /* OK button */
    0, 0,             /* never dimmed */
    0, 0,             /* never checked */
    0, 0              /* never marked */
    ,
    
    /* [2] */
    HMStringREsItem 
     10, 10 ,
     0, 0, 0, 0 ,
    500, 2,           /* Cancel button */
    0, 0,
    0, 0,
    0, 0
    ,
    
    /* [3] */
    HMSkipItem       /* Name text */
    ,

    /* [4] */
    HMSkipItem       /* Phone text */
    ,

    /* [5] */
    HMStringREsItem 
     10, 10 ,
     0, 0, 0, 0 ,
    500, 3,           /* Name field */
    0, 0,
    0, 0,
    0, 0
    ,
    
    /* [6] */
    HMStringREsItem 
     10, 10 ,
     0, 0, 0, 0 ,
    500, 4,           /* Phone text */
    0, 0,
    0, 0,
    0, 0
    ,

    /* [7] */
    HMStringREsItem 
     5, 5 ,
     0, 0, 0, 0 ,
    500, 5,           /* Male button */
    0, 0,
    500, 6,           /* checked */
    0, 0
    ,

    /* [8] */
    HMStringREsItem 
     3, 3 ,
     0, 0, 0, 0 ,
    500, 7,           /* Female button */
    0, 0,
    500, 8,           /* checked */
    0, 0
    ,

    /* [9] */
    HMSkipItem       /* Phone text */
    ,

    /* [10] */
    HMStringREsItem 
     10, 1 ,
     0, 0, 0, 0 ,
    500, 9,            /* Married box */
    0, 0,
    500, 10,           /* checked */
    0, 0
    ,

    /* [11] */
    HMStringREsItem 
     10, 10 ,
     0, 0, 0, 0 ,
    0, 0,
    0, 0,
    500, 11,          /* Popup */
    500, 12           /* Popup */
    ,
  
;

resource 'STR#' (500, "Sample Dialog Help Strings") 
  
  /* [1] */
  "To dismiss dialog and save changes, click this button.";
  /* [2] */
  "To dismiss dialog without saving changes, click this button.";
  /* [3] */
  "Enter persons name you wish to add to database.";
  /* [4] */
  "Enter persons phone number you wish to add "
  "to the database.";
  /* [5] */
  "Click this radio button to identify person as male.";
  /* [6] */
  "Identifies person as male.";
  /* [7] */
  "Click this radio button to identify person as female.";
  /* [8] */
  "Identifies person as female.";
  /* [9] */
  "Check this box if person is married.";
  /* [10] */
  "Identifies person as being married.";
  /* [11] */
  "Use pop-up menu to select a database to add person to."; 
  /* [12] */
  "Use pop-up menu to select a database to add person to."; 
  
;

// End of File